import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddMCQuestion extends JFrame implements ActionListener, FocusListener{
	
/*
(BorderLayout CENTER)
Row1:	Type the question here
Row2:	How many possible responses?
Row3:	Response 1
Row4:	Response 2
Row5:	Response 3
Row6:	Response 4
Row7:	Response 5
(BorderLayout SOUTH)
Row8:	Cancel and Submit Buttons
*/
	
	private JFrame mainFrame;
	private JPanel main, row1, row2, row3, row4, row5, row6, row7, row8, row9;
	private JButton cancelButton, submitButton;
	private JTextField question, response1, response2, response3, response4, response5;
	private JRadioButton radio1, radio2, radio3, radio4, radio5;
	private ButtonGroup buttonGroup;
	private JLabel	numberOfResponses, selectAResponse;
	private JComboBox comboBox;
	
	public AddMCQuestion(){
		super("Add a Multiple Choice Question");
		
		mainFrame = new JFrame("Add a Multiple Choice Question");
		main = new JPanel();
		main.setLayout(new BoxLayout(main,BoxLayout.Y_AXIS));
		row1 = new JPanel(new FlowLayout());
		row2 = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		row4 = new JPanel(new FlowLayout());
		row5 = new JPanel(new FlowLayout());
		row6 = new JPanel(new FlowLayout());
		row7 = new JPanel(new FlowLayout());
		row8 = new JPanel(new FlowLayout());
		
		question = new JTextField("Type your question here...");
		question.setPreferredSize(new Dimension(285,20));
		numberOfResponses = new JLabel("Select the number of possible responses: ");
		comboBox = new JComboBox(new String[] {"2","3","4","5"});
		comboBox.setSelectedIndex(3);
		buttonGroup = new ButtonGroup();
		radio1 = new JRadioButton();
		radio1.setFocusable(false);
		response1 = new JTextField("Type the first response here...");
		response1.setPreferredSize(new Dimension(275,20));
		response1.selectAll();
		radio2 = new JRadioButton();
		radio2.setFocusable(false);
		response2 = new JTextField("Type the second response here...");
		response2.setPreferredSize(new Dimension(275,20));
		radio3 = new JRadioButton();
		radio3.setFocusable(false);
		response3 = new JTextField("Type the third response here...");
		response3.setPreferredSize(new Dimension(275,20));
		radio4 = new JRadioButton();
		radio4.setFocusable(false);
		response4 = new JTextField("Type the fourth response here...");
		response4.setPreferredSize(new Dimension(275,20));
		radio5 = new JRadioButton();
		radio5.setFocusable(false);
		response5 = new JTextField("Type the fifth response here...");
		response5.setPreferredSize(new Dimension(275,20));
		buttonGroup.add(radio1);
		buttonGroup.add(radio2);
		buttonGroup.add(radio3);
		buttonGroup.add(radio4);
		buttonGroup.add(radio5);
		cancelButton = new JButton("Cancel");
		submitButton = new JButton("Submit");
		
		row1.add(question);
		row2.add(numberOfResponses);
		row2.add(comboBox);
		row3.add(radio1);
		row3.add(response1);
		row4.add(radio2);
		row4.add(response2);
		row5.add(radio3);
		row5.add(response3);
		row6.add(radio4);
		row6.add(response4);
		row7.add(radio5);
		row7.add(response5);
		row8.add(cancelButton);
		row8.add(submitButton);
		main.add(row1);
		main.add(row2);
		main.add(row3);
		main.add(row4);
		main.add(row5);
		main.add(row6);
		main.add(row7);
				
		// add action listeners to each of the buttons
		question.addFocusListener(this);
		comboBox.addActionListener(this);
		response1.addFocusListener(this);
		radio1.addActionListener(this);
		response2.addFocusListener(this);
		radio2.addActionListener(this);
		response3.addFocusListener(this);
		radio3.addActionListener(this);
		response4.addFocusListener(this);
		radio4.addActionListener(this);
		response5.addFocusListener(this);
		radio5.addActionListener(this);
		cancelButton.addActionListener(this);
	    submitButton.addActionListener(this);

		mainFrame.add(main, BorderLayout.CENTER);
		mainFrame.add(row8, BorderLayout.SOUTH);
		mainFrame.setSize(500,500);
		mainFrame.setVisible(true);
	}

	public void focusGained(FocusEvent e){
		if(e.getSource()==question){
			if(question.getText().equals("Type your question here...")){
				question.selectAll();
			}
		}else if(e.getSource()==response1){
			if(response1.getText().equals("Type the first response here...")){
				response1.selectAll();
			}
		}else if(e.getSource()==response2){
			if(response2.getText().equals("Type the second response here...")){
				response2.selectAll();
			}
		}else if(e.getSource()==response3){
			if(response3.getText().equals("Type the third response here...")){
				response3.selectAll();
			}
		}else if(e.getSource()==response4){
			if(response4.getText().equals("Type the fourth response here...")){
				response4.selectAll();
			}
		}else if(e.getSource()==response5){
			if(response5.getText().equals("Type the fifth response here...")){
				response5.selectAll();
			}
		}
	}
	
	public void focusLost(FocusEvent e){}
	
	public void actionPerformed( ActionEvent event){
		
		//Code for handling actions
		if(event.getSource()==comboBox){
			switch(comboBox.getSelectedIndex()){
			case 0:
				response3.setVisible(false);
				radio3.setVisible(false);
				response4.setVisible(false);
				radio4.setVisible(false);
				response5.setVisible(false);
				radio5.setVisible(false);
				break;
			case 1:
				response3.setVisible(true);
				radio3.setVisible(true);
				response4.setVisible(false);
				radio4.setVisible(false);
				response5.setVisible(false);
				radio5.setVisible(false);
				break;
			case 2:
				response3.setVisible(true);
				radio3.setVisible(true);
				response4.setVisible(true);
				radio4.setVisible(true);
				response5.setVisible(false);
				radio5.setVisible(false);
				break;
			case 3:
				response3.setVisible(true);
				radio3.setVisible(true);
				response4.setVisible(true);
				radio4.setVisible(true);
				response5.setVisible(true);
				radio5.setVisible(true);
				break;
			}
		}else if(event.getSource()==cancelButton){
			
			//Go to the last page--probably the page where a survey preview can be seen
			
		}else if(event.getSource()==submitButton){
			if(question.getText().equals("") || question.getText().equals("Type your question here...")){
				JOptionPane.showMessageDialog(this, "Please enter a question.", "Warning!", JOptionPane.WARNING_MESSAGE);
			}
			
			//Database interaction--submit data to database
			
		}
	}

	public static void main(String args[]){
		AddMCQuestion application = new AddMCQuestion();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}